package com.goktech.olala.client.controller.order;


import com.goktech.olala.core.service.ICustomerCollectService;
import com.goktech.olala.core.service.IShopCartService;
import com.goktech.olala.server.pojo.customer.Customer;
import com.goktech.olala.server.pojo.customer.CustomerCollect;
import com.goktech.olala.server.pojo.order.ShopCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Classname OrderCartController
 * @Description TODO
 * @Date 2020/12/21 20:11
 * @Created by ASUS
 */
@Controller
@RequestMapping("/orderCartApi")
public class OrderCartController {

    @Autowired
    IShopCartService shopCartService;
    @Autowired
    ICustomerCollectService customerCollectService;

    ModelAndView mav = new ModelAndView();


    //购物车页面
    @RequestMapping("/queryAllShopCart.do")
    public ModelAndView toShopCart(HttpServletRequest request){
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("LoginUser");

        if (customer==null){//好家伙，手动过滤器，又来啦
            mav.setViewName("home/login");
            return mav;
        }
        //拿到用户id
        String customerId = (String) session.getAttribute("customerId");

        //购物车商品
        List<ShopCart> shopCartList = shopCartService.selectByCustomerId(customerId);
        //收藏的商品
        List<CustomerCollect> customerCollectList = customerCollectService.selectByCustomerId(customerId);
        mav.addObject("shopCartVos", shopCartList);
        mav.setViewName("home/shopcart");
        return mav;
    }

    //添加到购物车
    @RequestMapping("/saveShopCart.do")
    public ModelAndView saveShopCart(HttpServletRequest request){

        HttpSession session = request.getSession();
        //拿到用户id
        String customerId = (String) session.getAttribute("customerId");

        /*
        source=1
        goodsId=1596677386
        goodsSn=159667738603
        goodsName=手剥松子218g%20坚果炒货%20巴西松子
        goodsAttr=
        parentGoodsId=
        shopPrice=2960
        goodsAmount=1
        realBuyPrice=2705
        marketPrice=3500*/
        //没有的就不获取了，数据库里面也没有。。。
//        String source = request.getParameter("source");
        String goodsId = request.getParameter("goodsId");
        String goodsSn = request.getParameter("goodsSn");

        String theGoodsName = request.getParameter("goodsName");
        String goodsName = null;
        if (theGoodsName!=null) {
            try {
                goodsName = URLDecoder.decode(theGoodsName, "utf-8");//转码成中文
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        String price = request.getParameter("shopPrice");
        String amount = request.getParameter("goodsAmount");
        String buyPrice = request.getParameter("realBuyPrice");
        String marketP = request.getParameter("marketPrice");

        Integer shopPrice = Integer.parseInt(price);
        Integer goodsAmount = Integer.parseInt(amount);
        Integer realBuyPrice = Integer.parseInt(buyPrice);
        Integer marketPrice = Integer.parseInt(marketP);

        //购物车类
//        RespShopCartVo shopCartVo = new RespShopCartVo();
//        shopCartVo.setCustomerId(customerId);
//        shopCartVo.setGoodsId(goodsId);
//        shopCartVo.setGoodsSn(goodsSn);
//        shopCartVo.setGoodsName(goodsName);
//        shopCartVo.setShopPrice(shopPrice);
//        shopCartVo.setGoodsAmount(goodsAmount);
//        shopCartVo.setRealBuyPrice(realBuyPrice);
//        shopCartVo.setMarketPrice(marketPrice);

        ShopCart shopCart = new ShopCart();
        shopCart.setCustomerId(customerId);
        shopCart.setGoodsId(goodsId);
        shopCart.setGoodsSn(goodsSn);
        shopCart.setGoodsName(goodsName);
        shopCart.setShopPrice(shopPrice);
        shopCart.setGoodsAmount(goodsAmount);
        shopCart.setRealBuyPrice(realBuyPrice);
        shopCart.setMarketPrice(marketPrice);

        //添加进数据库
        int insert = shopCartService.addShopCart(shopCart);

        mav.setViewName("redirect:/orderCartApi/queryAllShopCart.do");
        return mav;
    }

    //删除商品
    @RequestMapping(value = "/removeShopCart.do")
    public ModelAndView deleteShop(HttpServletRequest request){
        //拿到传来的购物车id
        String CartId = request.getParameter("shopCartId");
        Integer shopCartId = Integer.parseInt(CartId);
        //通过购物车id删除购物车
        int i = shopCartService.deleteByPrimaryKey(shopCartId);
        mav.setViewName("redirect:/orderCartApi/queryAllShopCart.do");
        return mav;
    }




}
